﻿$(function() {
    //$M.topMenusrefreshWidth();
    /*左边菜单*/
    $(document.body).on("click", "#sys_menu .menu-btn", function() {
        var _this = $(this);
        var id = _this.attr("data-id");
        var url = _this.attr("data-url");
        var title = _this.attr("data-title");
        var icon = _this.attr("data-icon");

        if (url === undefined) { url == "" };
        if (id === undefined) { id == "" };
        if (url != "" && id != "") {
            $(".menu-ul>li .menu-btn").removeClass("show");
            _this.addClass("active").addClass("show");
            $M.addTopMenu({ id: id, url: url, title: title, icon: icon });
            return;
        }
        $(".menu-ul>li ul>li ul>li .menu-btn").removeClass("active");
        if (_this.next("ul").is(":visible")) {
            _this.removeClass("active");
            _this.next("ul").slideUp();
            return;
        }


        _this.addClass("active");

        //把同级菜单关闭
        _this.parent("li").siblings().children("ul").slideUp();
        _this.next("ul").slideDown();

    });


    //菜单收缩按钮点击
    $("#pm-fast-btn").click(function() {
        var dataOpen = $(this).attr("data-open");
        if (dataOpen == "no") {
            $(".pm-content-left").animate({ marginLeft: 0 }, 500);
            $(this).attr("data-open", "yes");
            return;
        }
        $(this).attr("data-open", "no");
        $(".pm-content-left").animate({ marginLeft: -180 }, 500);
    });

    //快捷键
    $("#top-menus").on("click", ">li>a", function(e) {
        var _this = $(this);
        var id = _this.attr("data-id");
        var url = _this.attr("data-url");
        var title = _this.attr("data-title");

        _this.parent().siblings().children("a").removeClass("active");
        _this.addClass("active");
        $M.openPages({ id: id, url: url, title: title });
    });

    //头部快捷菜单删除被点击
    $("#top-menus").on("click", ">li span[data-top-menu='close']", function(e) {
        e.stopPropagation();
        $("#top-menus>li>a").removeClass("active");
        var _this = $(this);
        var closeId = _this.parent("a").attr("data-id");
        var prev = _this.parent("a").parent("li").prev(); //上一个节点
        var next = _this.parent("a").parent("li").next(); //
        var openId = "0";
        if (next.length > 0) {
            openId = next.children("a").attr("data-id");
        } else if (prev.length > 0) {
            openId = prev.children("a").attr("data-id");
        }
        _this.parent().parent("li").remove();
        $("#top-menus>li>a[data-id='" + openId + "']").addClass("active");
        $M.closePages({ open: openId, close: closeId });
    });

    $(window).resize(function() { //当浏览器大小变化时
        $M.topMenusrefreshWidth();
    });
})


var $M = {
    addTopMenu: function(o) {
        $("#top-menus>li>a").removeClass("active");
        var el = "#top-menus>li>a[data-id='{0}']".Format(o.id);
        if ($(el).length > 0) {
            $(el).addClass("active");
        } else {
            var _topLiHtml = '<li>' +
                ' <a class="active" data-id="{0}" data-url="{1}" data-title="{2}" >'.Format(o.id, o.url, o.title) +
                '     <span class="tm-icon"><i class="{0}"></i></span>'.Format(o.icon) +
                '     <span class="tm-text">{0}</span>'.Format(o.title) +
                '     <span data-top-menu="close">' +
                '         <i class="icon-times"></i>' +
                '     </span>' +
                ' </a>' +
                '</li>';
            $("#top-menus").append(_topLiHtml);
        }
        this.openPages(o);
        this.topMenusScroll(o.id);
    },
    closePages: function(parm) {
        var openPage = ".iframe-content-item[data-id='{0}']".Format(parm.open);
        var closePage = ".iframe-content-item[data-id='{0}']".Format(parm.close);
        $(closePage).remove();
        $(openPage).show().addClass("pages-in");
    },
    // {type:1,id:1}  1=关闭右边  2=关闭其它   3=关闭所有
    closeRightPages: function(parm) {
        var el = "#top-menus>li>a[data-id='{0}']".Format(parm.id);
        var lis;
        if (parm.type == 1) {
            var _this = $(el);
            lis = _this.parent().nextAll();
        } else if (parm.type == 2) {
            el = "#top-menus>li>a[data-id!='{0}']".Format(parm.id);

            lis = $(el).parent();
            console.log(lis);

        } else if (parm.type == 3) {
            el = "#top-menus>li>a[data-id!='0']";
            lis = $(el);
        }

        $(lis).each(function(i, dom) {
            var a = $(dom).children("a");
            var id = a.attr("data-id");
            var c = ".iframe-content-item[data-id='{0}']".Format(id);
            $(c).remove();
        });
        lis.remove();
    },
    openPages: function(parm) {
        $("#iframe-content>div").hide();
        var el = ".iframe-content-item[data-id='{0}']".Format(parm.id);
        $(el).removeClass("pages-out");
        if ($(el).length <= 0) {
            var iframeHtml = '<div class="iframe-content-item" data-id="{0}">'.Format(parm.id) +
                ' <iframe src="{0}"></iframe>'.Format(parm.url) +
                ' </div>';
            $("#iframe-content").append(iframeHtml);
        }
        $(el).show().addClass("pages-in");
        this.topMenusContextmenu(parm.id);
        this.topMenusScroll(parm.id);
    },
    topMenusScroll: function(id) {
        var m = $("#top-menus");
        var target = $("#top-menus>li>a[data-id='" + id + "']");
        $(m).animate({
            scrollLeft: $(target).offset().left
        }, 100);
    },
    topMenusrefreshWidth: function() {
        var mbWidth = $(".mission-board").width();
        $("#top-menus").css("width", mbWidth);
    },
    topMenusContextmenu: function(id) {

        var _this = this;
        //绑定右键菜单
        var dom = "#top-menus>li>a[data-id='{0}']".Format(id);
        var _thisMenu = $(dom).contextmenu({
            items: [{
                label: '关闭右边标签',
                url: "javascript:;",
                onClick: function(event) {
                    var id = parseInt(_thisMenu.attr("data-id"));
                    _this.closeRightPages({ type: 1, id: id })
                }
            }, {
                label: '关闭其它标签',
                url: "javascript:;",
                onClick: function(event) {
                    var id = parseInt(_thisMenu.attr("data-id"));
                    _this.closeRightPages({ type: 2, id: id })
                }
            }, {
                label: '关闭所有标签',
                url: "javascript:;",
                onClick: function(event) {
                    var id = parseInt(_thisMenu.attr("data-id"));
                    _this.closeRightPages({ type: 3, id: id })
                }
            }],
            onClickItem: function(item) {

            },
            animation: "fade scale"
        });
    }
}


function exitLogin() {

    $.layer.confirm("是否退出登录", function() {
        location.href = "/views/login.html";
    })
}